/**
 * 
 */
package com.teamall.web.tssy.service.impl;

import java.math.*;
import java.util.*;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teamall.core.common.Page;
import com.teamall.framework.util.IdCreateUtils;
import com.teamall.framework.util.StringUtils;
import com.teamall.core.convert.IArgConversionService;
import com.teamall.core.idproduce.ISequenceGenerator;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.web.tssy.db.arg.TsSyThirdArg;
import com.teamall.web.tssy.db.arg.TsSyThirdArg.TsSyThirdCriteria;
import com.teamall.web.tssy.db.dao.TsSyThirdDao;
import com.teamall.web.tssy.db.po.TsSyThirdPO;
import com.teamall.web.tssy.service.ITsSyThirdService;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.tssy.service.impl <br>
 */

@Service("tsSyThirdService")
public class TsSyThirdServiceImpl implements ITsSyThirdService {

    private static final LogManager logger = LogManager
            .getLogger(TsSyThirdServiceImpl.class);

    @Autowired
    private TsSyThirdDao tsSyThirdDao;
    

    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;
    

    @Override
    public TsSyThirdPO selectByPrimaryKey(String key) throws BaseAppException {
    	logger.debug("selectByPrimaryKey begin...id={0}", key);
        // ///////e
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return tsSyThirdDao.selectByPrimaryKey(key);
    }

    @Override
    public List<TsSyThirdPO> selectByArg(TsSyThirdPO record) throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        //TsSyThirdArg arg = new TsSyThirdArg();
        //TsSyThirdCriteria criteria = arg.createCriteria();
        
        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        record.addNewConditions("isDel", "F");
        TsSyThirdArg arg = argConversionService.invokeArg(TsSyThirdArg.class, record);

        // ///////
        // TODO 根据业务场景，设置查询条件，示例
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }
        // ///////

        return tsSyThirdDao.selectByArg(arg);
    }

    @Override
    public Page<TsSyThirdPO> selectByArgAndPage(TsSyThirdPO record, Page<TsSyThirdPO> resultPage)
            throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        // TsSyThirdArg arg = new TsSyThirdArg();
        // //TODO 根据业务场景，设置查询条件，示例
        // TsSyThirdCriteria criteria = arg.createCriteria();
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }
        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        TsSyThirdArg arg = argConversionService.invokeArg(TsSyThirdArg.class, record);

        resultPage = tsSyThirdDao.selectByArgAndPage(arg, resultPage);


        return resultPage;
    }

    @Override
    public int add(TsSyThirdPO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
    	String pkId =  IdCreateUtils.createId();
    	record.setId(pkId);
    	record.setCreateTime(new Date());
        record.setUpdateTime(new Date());

        return tsSyThirdDao.insertSelective(record);
    }

    @Override
    public int update(TsSyThirdPO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等
        record.setUpdateTime(new Date());
        // ///////

        return tsSyThirdDao.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int updateStatus(String id) throws BaseAppException {
        logger.debug("updateStatus begin...id={0}", id);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        String idStr = "";
        TsSyThirdPO po = new TsSyThirdPO();
        if(StringUtils.isNotEmpty(id)){
        	String[] ids = id.split(",");
            for(String tempId:ids){
            	idStr+="'"+tempId+"',";
            }
            po.setId(idStr.substring(0, idStr.length()-1));
            po.setIsDel("T");
            po.setUpdateTime(new Date());
        }else{
        	throw new BaseAppException("选择的记录ID为空");
        }
        
        return tsSyThirdDao.updateByPrimaryKeys(po);
        
    }

    @Override
    public int delete(TsSyThirdPO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////
        
        return tsSyThirdDao.deleteByPrimaryKey(record.getId());
    }

}
